package com.xinke.edu.mypetadoption.fragment2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.xinke.edu.mypetadoption.util.SPUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xinke.edu.mypetadoption.databinding.FragmentDdBinding;
import com.xinke.edu.mypetadoption.entity.OrdersUserProductDTO;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.fragment2.adapter.DdAdpter;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class DdFragment extends Fragment {

    private FragmentDdBinding binding;
    private DdAdpter ddAdpter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentDdBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 1️⃣ 初始化适配器
        ddAdpter = new DdAdpter();

        // 2️⃣ RecyclerView 竖向列表布局
        binding.recyclerview.setLayoutManager(
                new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        );
        binding.recyclerview.setAdapter(ddAdpter);

        // 4️⃣ 调用接口加载数据
        loadOrders();
    }

    private void loadOrders() {

        //获取登录用户id（确保与Login_Activity中存储的ID完全一致）
        int userId = SPUtils.getInstance(requireContext()).getInt("userId", 1);
        
        // 添加日志输出，便于调试验证ID值
        Log.d("DdFragment", "使用的用户ID: " + userId);
        
        // 直接使用获取到的ID查询订单
        RetrofitUtil.getApi().selectOrdersMerchant(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<OrdersUserProductDTO>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody<List<OrdersUserProductDTO>> result) {
                        List<OrdersUserProductDTO> list = result.getData();
                        if (list != null && !list.isEmpty()) {
                            ddAdpter.addAll(list); // BaseQuickAdapter 的添加方法
                        } else {
                            Toast.makeText(requireContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(requireContext(), "加载失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
